package org.cosmact.designer.vxml.gefeditor.editpart;

import java.util.List;

import org.cosmact.designer.vxml.gefeditor.editpart.policy.DialogComponentEditPolicy;
import org.cosmact.designer.vxml.gefeditor.editpart.policy.DialogGraphicalNodeEditPolicy;
import org.cosmact.designer.vxml.gefeditor.figures.DialogContainerFigure;
import org.cosmact.designer.vxml.gefeditor.uiresources.EIcons;
import org.cosmact.designer.vxml.gefeditor.uiresources.UIResource;
import org.cosmact.vxml.model.Dialog;
import org.cosmact.vxml.model.ModelPackage;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;

public class DialogEditPart extends AbstractVxmlEditPart implements
	NodeEditPart {

    @Override
    protected IFigure createFigure() {
	DialogContainerFigure iconFigure = new DialogContainerFigure(UIResource
		.getImage(EIcons.VOICE_CHAT32), getCastedModel().getId());
	iconFigure.setToolTip(new Label(getCastedModel().getId()));
	return iconFigure;
    }

    private void setBounds(IFigure figure) {
	figure.setSize(getAndSetSize());
	figure.setLocation(getCastedModel().getLocation());
    }

    @Override
    protected void createEditPolicies() {
	// allow removal of the associated model element
	installEditPolicy(EditPolicy.COMPONENT_ROLE,
		new DialogComponentEditPolicy());
	installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
		new DialogGraphicalNodeEditPolicy());
    }

    @Override
    protected List getModelSourceConnections() {
	return getCastedModel().getSourceConnections();
    }

    @Override
    protected List getModelTargetConnections() {
	return getCastedModel().getTargetConnections();
    }

    @Override
    public ConnectionAnchor getSourceConnectionAnchor(
	    ConnectionEditPart connection) {
	return new ChopboxAnchor(getFigure());

    }

    @Override
    public ConnectionAnchor getSourceConnectionAnchor(Request request) {
	return new ChopboxAnchor(getFigure());
    }

    @Override
    public ConnectionAnchor getTargetConnectionAnchor(
	    ConnectionEditPart connection) {
	return new ChopboxAnchor(getFigure());
    }

    @Override
    public ConnectionAnchor getTargetConnectionAnchor(Request request) {
	return new ChopboxAnchor(getFigure());
    }

    private Dialog getCastedModel() {
	return (Dialog) getModel();
    }

    @Override
    public void notifyChanged(Notification notification) {
	int featureId = notification.getFeatureID(ModelPackage.class);
	switch (featureId) {
	case ModelPackage.DIALOG__LOCATION:
	case ModelPackage.DIALOG__SIZE:
	    refreshVisuals();
	    break;
	case ModelPackage.DIALOG__SOURCE_CONNECTIONS:
	    refreshSourceConnections();
	    break;
	case ModelPackage.DIALOG__TARGET_CONNECTIONS:
	    refreshTargetConnections();
	    break;
	}
    }

    @Override
    public void refreshVisuals() {
	setBounds(getFigure());
	super.refreshVisuals();
    }

    private Dimension getAndSetSize() {
	if (getCastedModel().getSize() == null) {
	    Dimension defaultSize = new Dimension(
		    DialogContainerFigure.DIALOG_SIZE_WIDTH_DEFAULT,
		    DialogContainerFigure.DIALOG_SIZE_HEIGHT_DEFAULT);
	    getCastedModel().setSize(defaultSize);
	}
	return getCastedModel().getSize();
    }
}
